package com.wz.project.stock.stockForzen.controller;

import com.wz.common.utils.ServletUtils;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.domain.BasStockFrozenRecord;
import com.wz.project.stock.stockForzen.service.ForzenService;
import com.wz.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author jinhuijiang
 * @create 2021-10-25 9:57
 */
@Controller
@RequestMapping("/stock/stockForzen")
public class StockForzenController extends BaseController {
    private String prefix = "stock/stockForzen";

    @Autowired
    ForzenService forzenService;

//    @RequiresPermissions("stock:forzen:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/stockForzen";
    }

    /**
     * 修改库存页面
     * @return
     */
    @RequestMapping("/stockForzenRecord")
    public String stockForzenRecord()
    {
        return "/stock/stockForzen/stockForzenRecord";
    }

//    @RequiresPermissions("stock:forzen:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasStock basStock)
    {
        startPage();
        List<BasStock> list = forzenService.findStockList(basStock);
        return getDataTable(list);
    }

    /**
     * 冻结库存
     * @param ids
     * @return
     */
    @RequiresPermissions("stock:stcokForzen:freezeIn")
    @RequestMapping("/freezeIn")
    @ResponseBody
    public AjaxResult freezeInStock(String ids){
        User user = (User) ServletUtils.getSubject().getPrincipal();
        String userName = user.getUserName();
        return toAjax(forzenService.freezeInStock(ids,userName));
    }

    /**
     * 解冻库存
     * @param ids
     * @return
     */
    @RequiresPermissions("stock:stcokForzen:unfreeze")
    @RequestMapping("/unfreeze")
    @ResponseBody
    public AjaxResult unfreezeStock(String ids){
        User user = (User) ServletUtils.getSubject().getPrincipal();
        String userName = user.getUserName();
        return toAjax(forzenService.unfreezeStock(ids,userName));
    }

//    @RequiresPermissions("stock:forzen:listRecord")
    @PostMapping("/listRecord")
    @ResponseBody
    public TableDataInfo list(BasStockFrozenRecord record)
    {
        if ("".equals(record.getCargoSpaceId())){
            record.setCargoSpaceId(null);
        }
        if ("".equals(record.getProductId())){
            record.setProductId(null);
        }
        if ("".equals(record.getTrayCode())){
            record.setTrayCode(null);
        }
        startPage();
        List<BasStockFrozenRecord> list = forzenService.findFrozenList(record);
        return getDataTable(list);
    }

}
